<?php
namespace Common\Models;
use Common\BaseModel;
class BShopOrder extends BaseModel
{
    public $res;
    public $expire;

    public function initialize()
    {
        parent::initialize();
        $this->setSource('b_shop_order');
    }

    /**
     * 批量修改订单状态
     */
    public function status_amount_pay($order_count_sn){
        $get_order_list = $this->find(array(
            "conditions"=>"order_count_sn='{$order_count_sn}'",
        ))->update(['status'=>1]);
        return $get_order_list;
    }




    public function get_list($params){
        $page = intval($params['page']);
        $size = intval($params['limit']);
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $condtions="o.user_id={$params['user_id']} AND o.status<>-1";
        if(isset($params['status']) && $params['status'] != ''){
            $condtions .= " AND o.status in ({$params['status']})";
        }
        $sql="
        SELECT o.order_sn,o.unit_price,o.end_price,o.num,o.status,o.goods_name,o.created_at,o.goods_img,o.expired_at,sku.value_ids,s.shop_name,s.shop_avatar FROM `b_shop_order` as o
left join f_users as u on u.id=o.user_id
left join b_shop_goods as g on g.id=o.goods_id
left join b_shop_users_info as s on s.shop_id=o.shop_id
left join b_shop_goods_sku as sku on sku.id=o.goods_sku_id
where $condtions
order by o.created_at desc
limit $page,$size";
        $items = $this->getDI()->get('dbMaster')->fetchAll($sql);
        $BShopSkuValueDb = new BShopSkuValue();
        foreach($items as $key=>$value){
            $items[$key]['sku_value_name'] = $BShopSkuValueDb->getValueName($value['value_ids']);
            $items[$key]['expired_time'] = strtotime($value['expired_at']) - time() < 0 ? 0 : strtotime($value['expired_at']) - time();
        }
        return $items;
    }



    public function get_order_detail($order_sn){
        $condtions="o.order_sn='{$order_sn}'";
        $sql="
        SELECT o.id,o.order_sn,o.expired_at,o.shop_id,u.nickname,u.phone,su.shop_name,su.shop_avatar,u.id as user_id,ad.name as ad_name,ad.phone as ad_phone,ad.address,o.unit_price,o.end_price as total_price,o.num,o.status,o.express_id,e.express_name,e.express_sn,e.express_logo,e.created_at as express_created_at,o.created_at,o.pay_type,o.pay_time,o.message,g.goods_sn,g.name as goods_name,g.detailed_img,g.original_price,sku.endprice,sku.value_ids,g.goods_sn FROM `b_shop_order` as o
left join f_users as u on u.id=o.user_id
left join b_shop_goods as g on g.id=o.goods_id
left join b_shop_goods_sku as sku on sku.id=o.goods_sku_id
left join b_shop_order_address as ad on ad.id=o.address_id
left join b_shop_order_express as e on e.id=o.express_id
left join b_shop_users_info as su on su.shop_id=o.shop_id
where $condtions";
        $detail = $this->getDI()->get('dbMaster')->fetchOne($sql);
        if($detail){
            $sku_ids = str_replace('_',',',$detail['value_ids']);
            if(empty($sku_ids)){
                $sku_ids = "-1";
            }
            $BShopSkuValueDb = new BShopSkuValue();
            $value_name_arr = $BShopSkuValueDb->find(array(
                'conditions'=>"id in($sku_ids)",
                'columns'=>"value_name"
            ))->toarray();
            if($value_name_arr){
                $detail['sku_value_name'] = implode('-',array_column($value_name_arr,'value_name'));
            }else{
                $detail['sku_value_name'] = "";
            }
            $detail['expired_time'] = strtotime($detail['expired_at']) - time() < 0 ? 0 : strtotime($detail['expired_at']) - time();
        }
        return $detail;
    }


    //查询未付款的订单
    public function nopay(){
        $datetime=date('Y-m-d H:i:s');
        $condtions="expired_at<='{$datetime}' AND status=0";
        $get_list = $this->find(array(
            $condtions,
            'limit'=>100,
        ));
        return $get_list;
    }


}
